<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\ActiveForm;
use yii\widgets\LinkPager;
$this->title = '订单管理';
?>
<?= Html::cssFile('./assets/date/bootstrap-datetimepicker.min.css')?>
<div class="container">
	<div class="row">
	<?php echo $this->render('_leftnav') ?>
		<div class="col-md-10">
			<div class="page-header clearfix">
			<h1 class="pull-left">课程订单</h1>
				<div class="pull-right">
				</div>
			</div>
	<?php 
		$form = ActiveForm::begin([
			'options' => ['class' => 'form-inline well well-sm',
			'id'=>'comment-search-form'],
			'action'=>'/index.php?r=order/index',
			'method'=>'post',
			'fieldConfig' => [
				'template' => '<div class="form-group"><div class="col-md-8 controls"  style="padding-top: 9px">{input}{error}</div></div>'
			], 
		]);
	?>
		<div class="mbm">
			<div class="form-group">
				<label class="ptm">创建时间:</label>
				<?= Html::input('text', 'startDateTime',$order['startDateTime'],['class' => 'form-control form-datetime','id' => 'startDateTime','placeholder' => '起始时间']) ?>-
				<?= Html::input('text', 'endDateTime',$order['endDateTime'],['class' => 'form-control form-datetime','id' => 'endDateTime','placeholder' => '结束时间']) ?>
			</div>
		</div>
		<div class="mbm">
			<div class="form-group">
				<label class="ptm">筛选条件:</label>
				<?php 
					$Status=['' => '订单状态','0' => '未支付','1' => '已支付','2' => '订单关闭','3' => '成功'];
				?>
				<?= Html::dropDownList('orderStatus', $order['orderStatus'], ArrayHelper::map($Status,'id', 'orderStatus'), ['class' => 'form-control orderStatus']);?>
				<?php 
					$pay=['' => '支付方式','1' => '支付宝','2' => '微信支付','3' => '快捷支付'];
				?>
				<?= Html::dropDownList('payType', $order['payType'], ArrayHelper::map($pay,'id', 'payType'), ['class' => 'form-control payType']);?>
			</div> 
			<div class="form-group">
				<label class="ptm">关键词:</label>
				<?php 
					$keywordType=['' => '搜索方式','ordersn' => '订单号','username' => '购买者用户名'];
				?>
				<?= Html::dropDownList('keywordType', $order['keywordType'], ArrayHelper::map($keywordType,'id', 'keywordType'), ['class' => 'form-control select keywordType']);?>
				<?= Html::input('text', 'keyword', $order['keyword'], ['class' => 'form-control search','id' => 'keyword','placeholder' => '关键词']) ?>
			</div>
			<!-- <button class="btn btn-primary searchsub">搜索</button> -->
			<?= Html::submitButton('搜索', ['class' => 'btn btn-primary searchsub']) ?>
			<a class="btn btn-primary" href="javascript:;" id="export">导出结果</a>
		</div>
	<?php ActiveForm::end(); ?> 

<table class="table table-striped table-hover" id="order-table">
	<thead>
		<tr>
			<th>订单号</th>
			<th>创建时间</th>
			<th>状态</th>
			<th>实付(元)</th>
			<th>购买者</th>
			<th>支付方式</th>
			<th>操作</th>
		</tr>
	</thead>
	<?php if($data){?>
	<tbody>
		<?php foreach($data as $k=>$v){?>
		<tr id="order-table-45" style="word-break: break-all;word-wrap: break-word;">
			<td>
				<span class="text-muted text-sm"><?php echo $v['order_sn']?></span>
			</td>
			<td>
				<span class="text-muted text-sm"><?php echo date("Y-m-d H:i:s",$v['order_addtime'])?></span>
			</td>
			<td>
				<?php if($v['order_status']==0){
				?>
					<span class="label label-danger">未支付</span>
				<?php
				}else if($v['order_status']==1){
				?>
					<span class="label label-success">已付款</span>
				<?php
				}else if($v['order_status']==2){
				?>
					<span class="label label-danger">订单关闭</span>
				<?php
				}else if($v['order_status']==3){
				?>
					<span class="label label-success">成功</span>
				<?php }?>
			
			</td>
			<td>
				<span class="money-text"><?php echo $v['price_amount']?></span>
			</td>
			<td>
				<a class="link-light " href="#"><?php echo $v['user_name']?></a>
			</td>
			<td>
				<?php if($v['pay_type']==0){
					echo "--";
				}else if($v['pay_type']==1){
					echo "支付宝";
				}else if($v['pay_type']==2){
					echo "微信支付";
				}else if($v['pay_type']==3){
					echo "快捷支付";
				}?>
				<br>
				<span class="text-muted text-sm"><?php echo date("Y-m-d H:i:s",$v['pay_time'])?></span>
			</td>
			<td>
				<a href="javascript:;" class="btn btn-sm btn-default orderinfo" data-target="#OrderInfo" data-toggle="modal" type="<?php echo $v['order_id']?>">详情</a>
			</td>
		</tr>
		<?php }?>
	</tbody>
		<?php }else{?>
		<tr>
			<td colspan="20">
				<div class="empty">暂无订单记录</div>
			</td>
		</tr>
		<?php }?>
</table>
<div class="modal in" id="OrderInfo"></div>
			<div align="right">
					<?php echo LinkPager::widget([
						'pagination' => $pagination,
						'firstPageLabel'=>"首页",
						'prevPageLabel'=>'上一页',
						'nextPageLabel'=>'下一页',
						'lastPageLabel'=>'尾页',
					]);?>
			</div>
		</div>
	</div>
</div>
<?= Html::jsFile('./assets/date/bootstrap-datetimepicker.min.js')?>
<?= Html::jsFile('./assets/date/bootstrap-datetimepicker.fr.js')?>
<script type="text/javascript">
	$('.form-datetime').datetimepicker({
		autoclose: 1,//当选择一个日期之后是否立即关闭此日期时间选择器
		todayHighlight: 1,//如果为true, 高亮当前日期
		startView: 2,//日期时间选择器打开之后首先显示的视图  控制样式  0~4 
	});
	//详情页 
	$(".orderinfo").click(function(){
		var order_id=$(this).attr("type");
		$.ajax({
			type: "POST",
			url: "/index.php?r=order/orderinfo",
			data: "order_id="+order_id,
			success: function(msg){
				$("#OrderInfo").html(msg);
			}
		});
	})
	//搜索关键字验证
	$(".search").blur(function(){
		var search = $(this).val();
		var select = $(".select").val();
		if(select == ""){
			if(search != ""){
				alert("请选择搜索方式")
				$(".searchsub").attr("disabled",true)
			}
		}else if(select == "ordersn"){
			var reg=/^\d*$/
			if(!reg.test(search)){
				alert("订单号必须是数字")
				$(".searchsub").attr("disabled",true)
			}else{
				$(".searchsub").attr("disabled",false)
			}
		}else{
			$(".searchsub").attr("disabled",false)
		}
	})
	//下拉验证 
	$(".select").blur(function(){
		var search = $(".search").val();
		var select = $(this).val();
		if(select == ""){
			if(search != ""){
				alert("请选择搜索方式")
				$(".searchsub").attr("disabled",true)
			}
		}else if(select == "ordersn"){
			var reg=/^\d*$/
			if(!reg.test(search)){
				alert("订单号必须是数字")
				$(".searchsub").attr("disabled",true)
			}else{
				$(".searchsub").attr("disabled",false)
			}
		}else{
			$(".searchsub").attr("disabled",false)
		}
	})
	//导出
	$("#export").click(function(){
		var startDateTime=$("#startDateTime").val();
		var endDateTime=$("#endDateTime").val();
		var orderStatus=$(".orderStatus").val();
		var payType=$(".payType").val();
		var keywordType=$(".keywordType").val();
		var keyword=$("#keyword").val();
		location.href="<?php echo \Yii::$app->urlManager->createUrl('order/export')?>&startDateTime="+startDateTime+"&endDateTime="+endDateTime+"&orderStatus="+orderStatus+"&payType="+payType+"&keywordType="+keywordType+"&keyword="+keyword;
	})
</script>